package com.huha.notebook;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class NotebookController implements Initializable {
    @FXML
    TextArea txtContent;

    @FXML
    Button btnSave;

    // 程序初次加载时会执行的方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            // 将文件中的内容读取展示到文本控件
            txtContent.setText(FileUtil.readFile("data.txt"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @FXML
    void onSaveClicked(ActionEvent event){
        if("保 存".equals(btnSave.getText())){
            try {
                FileUtil.writeFile("data.txt", txtContent.getText());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            btnSave.setText("编 辑");
            txtContent.setEditable(false);
        }
        else{
            btnSave.setText("保 存");
            txtContent.setEditable(true);
        }
    }
}
